package com.masterlu.module.trade.controller.app.order2;

import com.masterlu.module.trade.controller.app.order2.vo.PackageOrderCreateReqVO;
import com.masterlu.module.trade.controller.app.order2.vo.PackageOrderCreateRespVO;
import com.masterlu.module.trade.controller.app.order2.vo.PackageOrderPageReqVO;
import com.masterlu.module.trade.controller.app.order2.vo.PackageOrderRespVO;
import com.masterlu.module.trade.dal.dataobject.order2.PackageOrderDO;
import com.masterlu.module.trade.service.order2.PackageOrderService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import com.masterlu.framework.common.pojo.PageParam;
import com.masterlu.framework.common.pojo.CommonResult;
import com.masterlu.framework.common.util.object.BeanUtils;
import static com.masterlu.framework.common.pojo.CommonResult.success;
import com.masterlu.framework.excel.core.util.ExcelUtils;
import com.masterlu.framework.apilog.core.annotation.ApiAccessLog;
import static com.masterlu.framework.apilog.core.enums.OperateTypeEnum.*;
import static com.masterlu.framework.common.util.collection.CollectionUtils.convertSet;
import static com.masterlu.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;


@Tag(name = "管理后台 - 套餐订单")
@RestController
@RequestMapping("/package/order")
@Validated
public class PackageOrderController {

    @Resource
    private PackageOrderService orderService;

    @PostMapping("/create")
    @Operation(summary = "创建套餐订单")
/*    @PreAuthorize("@ss.hasPermission('package:order:create')")*/
    public CommonResult<PackageOrderCreateRespVO> createOrder(@Valid @RequestBody PackageOrderCreateReqVO createReqVO) {

        PackageOrderDO order = orderService.createOrder(getLoginUserId(), createReqVO);
        return success(new PackageOrderCreateRespVO().setId(order.getId()).setPayOrderId(order.getPayOrderId()));
    }

    @PutMapping("/update")
    @Operation(summary = "更新套餐订单")
/*    @PreAuthorize("@ss.hasPermission('package:order:update')")*/
    public CommonResult<Boolean> updateOrder(@Valid @RequestBody  PackageOrderCreateReqVO updateReqVO) {
        orderService.updateOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除套餐订单")
    @Parameter(name = "id", description = "编号", required = true)
 /*   @PreAuthorize("@ss.hasPermission('package:order:delete')")*/
    public CommonResult<Boolean> deleteOrder(@RequestParam("id") Long id) {
        orderService.deleteOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得套餐订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
/*    @PreAuthorize("@ss.hasPermission('package:order:query')")*/
    public CommonResult<PackageOrderRespVO> getOrder(@RequestParam("id") Long id) {
        PackageOrderDO order = orderService.getOrder(id);
        return success(BeanUtils.toBean(order,  PackageOrderRespVO.class));
    }


   @GetMapping("/export-excel")
    @Operation(summary = "导出套餐订单 Excel")
/*    @PreAuthorize("@ss.hasPermission('package:order:export')")*/
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrderExcel(@Valid PackageOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PackageOrderDO> list = orderService.getOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "套餐订单.xls", "数据", PackageOrderRespVO.class,
                        BeanUtils.toBean(list, PackageOrderRespVO.class));
    }

}